package com.ccb.sc.common.modules.goods.service.impl;

import com.ccb.sc.common.modules.goods.convert.GoodsConvert;
import com.ccb.sc.common.modules.goods.entity.*;
import com.ccb.sc.common.modules.goods.mapper.*;
import com.ccb.sc.common.modules.goods.entity.*;
import com.ccb.sc.common.modules.goods.mapper.*;
import com.ccb.sc.common.modules.goods.service.IGoodsService;
import com.ccb.sc.common.modules.goods.vo.GoodsDetailVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ken
 * @version 1.0
 * @date 2020-09-20
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    private GoodsCarouselMapper goodsCarouselMapper;
    private GoodsSpecsMapper goodsSpecsMapper;
    private GoodsDetailMapper goodsDetailMapper;
    private BrandMapper brandMapper;
    private CategoryMapper categoryMapper;

    @Override
    public List<GoodsCarousel> getGoodsCarousels(Long goodsId) {
        return this.goodsCarouselMapper.selectList(new QueryWrapper<GoodsCarousel>().lambda().eq(GoodsCarousel::getGoodsId, goodsId));
    }

    @Override
    public List<GoodsSpecs> getGoodsSpecsByGoodsId(Long goodsId) {
        return this.goodsSpecsMapper.selectList(new QueryWrapper<GoodsSpecs>().lambda().eq(GoodsSpecs::getGoodsId, goodsId));
    }

    @Override
    public GoodsDetail getGoodsDetailByGoodsId(Long goodsId) {
        return this.goodsDetailMapper.selectOne(new QueryWrapper<GoodsDetail>().lambda().eq(GoodsDetail::getGoodsId, goodsId));
    }

    @Override
    public GoodsDetailVo detail(Long goodsId) {
        Goods goods = this.getById(goodsId);
        GoodsDetailVo goodsVo = GoodsConvert.INSTANCE.entity2DetailVo(goods);
        Brand brand = this.brandMapper.selectById(goods.getBrandId());
        if (brand != null) {
            goodsVo.setBrandName(brand.getName());
        }
        Category category = this.categoryMapper.selectById(goods.getCategoryId());
        if (category != null) {
            goodsVo.setCategoryName(category.getName());
        }
        List<GoodsCarousel> carousels = this.getGoodsCarousels(goodsId);
        if (!CollectionUtils.isEmpty(carousels)) {
            goodsVo.setCarousels(carousels.stream().map(GoodsCarousel::getUrl).collect(Collectors.toList()));
        }
        List<GoodsSpecs> specs = this.getGoodsSpecsByGoodsId(goodsId);
        if (!CollectionUtils.isEmpty(specs)) {
            goodsVo.setSpecs(GoodsConvert.INSTANCE.goodsSpec2VoBatch(specs));
        }
        GoodsDetail goodsDetail = this.getGoodsDetailByGoodsId(goodsId);
        if (goodsDetail != null) {
            goodsVo.setDetail(goodsDetail.getDetail());
        }
        return goodsVo;
    }


    @Autowired
    public void setGoodsCarouselMapper(GoodsCarouselMapper goodsCarouselMapper) {
        this.goodsCarouselMapper = goodsCarouselMapper;
    }

    @Autowired
    public void setGoodsSpecsMapper(GoodsSpecsMapper goodsSpecsMapper) {
        this.goodsSpecsMapper = goodsSpecsMapper;
    }

    @Autowired
    public void setGoodsDetailMapper(GoodsDetailMapper goodsDetailMapper) {
        this.goodsDetailMapper = goodsDetailMapper;
    }

    @Autowired
    public void setBrandMapper(BrandMapper brandMapper) {
        this.brandMapper = brandMapper;
    }

    @Autowired
    public void setCategoryMapper(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }
}
